﻿using QLHREntities;
using QLHuongRungBUS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLHuongRung
{
    public partial class LapDonDatHang : Form
    {
        private ThucDonBUS thucdonBUS = new ThucDonBUS();
        private MonAnBUS monanBUS = new MonAnBUS();
        private NhaCungCapBUS nhacungcapBUS = new NhaCungCapBUS();
        private NguyenLieuBUS nguyenlieuBUS = new NguyenLieuBUS();
        private DonDatHangBUS dondathangBUS = new DonDatHangBUS();

        private int maNhaCC = -1;
        private int maThucDon = -1;
        decimal tongTien = 0;

        private List<DonDatHang> donDatHang = new List<DonDatHang>();
        private List<NguyenLieuDatHang> list_nguyenlieu;
        private bool isLoadingComplete = false;
        public LapDonDatHang()
        {
            InitializeComponent();
        }

        private void SetDataComboBoxDatHang()
        {
            List<ThucDon> list_thucdon = thucdonBUS.LayDanhSachDatHang();
            var dataThucDon = new BindingList<KeyValuePair<int, string>>();

            foreach (var item in list_thucdon)
            {
                dataThucDon.Add(new KeyValuePair<int, string>(item.MaThucDon, ((DateTime)item.NgayBatDau).ToString("dd/MM/yyyy") + " ~ " + ((DateTime)item.NgayKetThuc).ToString("dd/MM/yyyy")));
            }
            cbbDatHang.DataSource = null;
            cbbDatHang.DataSource = dataThucDon;
            cbbDatHang.ValueMember = "Key";
            cbbDatHang.DisplayMember = "Value";
            //cbbDatHang.SelectedIndex = 0;
            isLoadingComplete = true;
        }

        private void LapDonDatHang_Load(object sender, EventArgs e)
        {
            SetDataComboBoxDatHang();
            SetDataComboBoxNhaCungCap();
            isLoadingComplete = true;
        }

        private void SetDataComboBoxNhaCungCap()
        {
            List<NhaCungCap> list_nhacungcap = nhacungcapBUS.LayDanhSachNhaCungCap();
            var dataNhaCungCap = new BindingList<KeyValuePair<int, string>>();
            foreach (var item in list_nhacungcap)
            {
                dataNhaCungCap.Add(new KeyValuePair<int, string>(item.MaNCC, item.TenNhaCungCap ));
            }
            ccbNhaCungCap.DataSource = null;
            ccbNhaCungCap.DataSource = dataNhaCungCap;
            ccbNhaCungCap.ValueMember = "Key";
            ccbNhaCungCap.DisplayMember = "Value";
            //cbbDatHang.SelectedIndex = 0;
        }

        private void onChange_ThayDoiDatHang(object sender, EventArgs e)
        {
            if (isLoadingComplete)
            {
                KeyValuePair<int, string> selectedEntry = (KeyValuePair<int, string>)((ComboBox)sender).SelectedItem;
                maThucDon = selectedEntry.Key;
                list_nguyenlieu = nguyenlieuBUS.LayDanhSachNguyenLieu(maThucDon);
                //if (!loadfirst)
                //    dGvDonDatHang.Columns.RemoveAt(8);
                //loadfirst = false;
                dGvDonDatHang.DataSource = null;
                dGvDonDatHang.DataSource = list_nguyenlieu.Where(m => m.SoLuongTon - m.SoLuongCan < m.SoLuongTonToiThieu).ToList();
                dGvDonDatHang.Columns[0].Visible = false;
                dGvDonDatHang.Columns[1].HeaderText = "Tên món ăn";
                dGvDonDatHang.Columns[2].HeaderText = "Tên nguyên liệu";
                dGvDonDatHang.Columns[3].HeaderText = "Gía";
                dGvDonDatHang.Columns[4].HeaderText = "Số lượng tồn";
                dGvDonDatHang.Columns[5].HeaderText = "Số lượng tồn tối thiểu";
                dGvDonDatHang.Columns[6].HeaderText = "Số lượng cần";
                dGvDonDatHang.Columns[7].HeaderText = "Số lượng đặt";
                dGvDonDatHang.Columns[8].Visible = false; // NhaCungCap
            
            }
        }

        private void dGvDatHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }


        private void onChange_ThayDoiNhaCungCap(object sender, EventArgs e)
        {
            if (dGvDonDatHang.DataSource != null)
            {
                KeyValuePair<int, string> selectedEntry = (KeyValuePair<int, string>)((ComboBox)sender).SelectedItem;
                maNhaCC = selectedEntry.Key;
                dGvDonDatHangCuoi.DataSource = list_nguyenlieu.Where(m => m.NhaCungCap == maNhaCC && m.SoLuongTon - m.SoLuongCan < m.SoLuongTonToiThieu).ToList();
                dGvDonDatHangCuoi.Columns[0].Visible = false;
                dGvDonDatHangCuoi.Columns[8].Visible = false;

                dGvDonDatHangCuoi.Columns[1].ReadOnly = true;
                dGvDonDatHangCuoi.Columns[2].ReadOnly = true;
                dGvDonDatHangCuoi.Columns[3].ReadOnly = true;
                dGvDonDatHangCuoi.Columns[4].ReadOnly = true;
                dGvDonDatHangCuoi.Columns[5].ReadOnly = true;
                dGvDonDatHangCuoi.Columns[6].ReadOnly = true;
                dGvDonDatHangCuoi.Columns[9].ReadOnly = true;
            }                       
        }

        private void btnDatHang_Click(object sender, EventArgs e)
        {
            if(dGvDonDatHangCuoi.DataSource == null)
            {
                MessageBox.Show("Chưa chọn nguyên liệu đặt hàng.");
                return;
            }
            int t = 0;
            for(int i = 0; i < dGvDonDatHangCuoi.Rows.Count; i++)
            {
                if (Int32.Parse(dGvDonDatHangCuoi.Rows[i].Cells[7].Value.ToString()) == 0)
                {
                    t = i + 1;
                    MessageBox.Show("Dòng thứ " + t + " chưa nhập đặt hàng!");
                    return;
                }
            }
            
            // Đơn đặt hàng
            DonDatHang donDatHang = new DonDatHang();
            int size = dGvDonDatHangCuoi.Rows.Count;
            if (maNhaCC != -1) 
            {
                donDatHang.NhaCungCap = maNhaCC;
            }
            donDatHang.ThoiDiemGiao = dtNgayGiao.Value;
            donDatHang.NgayDatHang = System.DateTime.Now;
            //decimal tongTien = 0;
            //for (int i = 0; i < size; i++)
            //{
            //    int soLuongDat = Int32.Parse(dGvDonDatHangCuoi.Rows[i].Cells[7].Value.ToString());
            //    decimal gia = Int32.Parse(dGvDonDatHangCuoi.Rows[i].Cells[3].Value.ToString());
            //    tongTien += soLuongDat * gia;
            //}
            donDatHang.TongTien = tongTien;
            donDatHang.TinhTrangGiao = 0;
            dondathangBUS.ThemDonDatHang(donDatHang);

            int maDDH = dondathangBUS.LayMaDDH();
            // Chi tiết đơn đặt hàng
            List<ChiTietDonDatHang> list_ChiTietDonDatHang = new List<ChiTietDonDatHang>();
            for (int j = 0; j < size; j++)
            {
                ChiTietDonDatHang item = new ChiTietDonDatHang();
                item.MaDDH = maDDH;
                item.ChiTietThucDon = Int32.Parse(dGvDonDatHangCuoi.Rows[j].Cells[9].Value.ToString());
                list_ChiTietDonDatHang.Add(item);
            }
            dondathangBUS.ThemChiTietDonDatHang(size, list_ChiTietDonDatHang);
            
            // Chi Tiết thực đơn
            List<ChiTietThucDon> list_ChiTietThucDon = new List<ChiTietThucDon>();
            for (int k = 0; k < size; k++)
            {
                ChiTietThucDon chiTietThucDon = new ChiTietThucDon();
                chiTietThucDon.MaCTTD = Int32.Parse(dGvDonDatHangCuoi.Rows[k].Cells[9].Value.ToString());
                chiTietThucDon.SoLuongDat = Int32.Parse(dGvDonDatHangCuoi.Rows[k].Cells[7].Value.ToString());
                list_ChiTietThucDon.Add(chiTietThucDon);
            }
            dondathangBUS.ThemChiTietThucDon(size, list_ChiTietThucDon);

            // Cập nhật tình trạng đặt hàng của thực đơn
            if(maThucDon != -1)
            {                
                thucdonBUS.CapNhatDangDatHang(maThucDon);
            }

            dGvDonDatHangCuoi.DataSource = null;
            dGvDonDatHang.DataSource = null;
            txtTongTien.Text = "";
            isLoadingComplete = false;
            SetDataComboBoxDatHang();

            MessageBox.Show("Thao tác thành công!");
        }

        private void onChange_SoLuongDat(object sender, DataGridViewCellEventArgs e)
        {
          //  if (e.ColumnIndex == ((DataGridView)sender).Columns[6].Index)
           // {
                int soLuongTon = (int)((DataGridView)sender)[4, e.RowIndex].Value;
                int soLuongTonToiThieu = (int)((DataGridView)sender)[5, e.RowIndex].Value;
                int soLuongCan = (int)((DataGridView)sender)[6, e.RowIndex].Value;
                decimal donGia = (decimal)((DataGridView)sender)[3, e.RowIndex].Value;

                int soLuongDat =  (int)((DataGridView)sender)[7, e.RowIndex].Value;
                //MessageBox.Show(((DataGridView)sender)[6, e.RowIndex].Value.ToString());  
                if ((soLuongDat + soLuongTon - soLuongCan) < soLuongTonToiThieu)
                {                    
                    MessageBox.Show("Cần đặt nguyên liệu nhiều hơn!");
                    ((DataGridView)sender)[7, e.RowIndex].Value = 0;
                    return;
                }
                tongTien += soLuongDat * donGia;
                txtTongTien.Text = tongTien.ToString();
            //}

        }

 
        private void btnHuyDon(object sender, EventArgs e)
        {
            this.Close();
        }  
    }
}
